var orgId = -1;
$(document).ready(function(){
    initData();
    setTimeout(function () {
        /**初始化站点**/
        initOrigin();
    }, 500);

    $('.clear-cache-btn').on('click', function () {
        wapLoading.show();
        $.ajax({
            type: "POST",
            url: "/sys_user/clear/cache",
            async: true,
            dataType: 'json',
            timeout: 5000,
            success: function(res) {
                wapLoading.hide();
                console.log(res)
                if(res.code == 0) {
                    wapAlert('缓存已更新');
                } else {
                    wapAlert(res.msg);
                    return;
                }

            },
            error: function(res) {
                wapAlert(res.msg);
                wapLoading.hide();
                return;
            },
            complete: function(XMLHttpRequest, status) {
                if(status == 'timeout') {
                    wapAlert("请求超时");
                    wapLoading.hide();
                    return;
                }
            }
        });
    });

    $('.foot-menu').on('click', function () {
       var url = $(this).attr('data-url');
       if (!isNull(url))
           window.location.href = url;
    });

    $('.navigation-menu').on('click', function () {
       wapAlert('暂未开放功能~!')
    });

    $('.role-menu').on('click', function () {
        var url = $(this).attr('data-url');
        if (!isNull(url))
            window.location.href = url;

    });

    /**点击站点选项**/
    $('.station-list').on('click', 'dt', function () {
        $(this).siblings().find(".icon-confirm").removeClass("confirm-yes");
        $(this).find(".icon-confirm").addClass("confirm-yes");
        orgId = $(this).find(".icon-confirm").attr('data-id');
    });

    $('.cancel-station').on('click', function () {
        $('.TipsWindown').hide();
    });

    /**点击站点**/
    $('.toolbar-station-btn').on('click', function () {
        $('.TipsWindown').show();
    });

    /**选择站点**/
    $('.confirm-station').on('click', function () {
        if (isNull(orgId) || orgId == -1) {
            wapAlert("请选择站点！");
            return;
        }
        wapLoading.show();
        $.ajax({
            type: "POST",
            url: "/console/chose/station",
            async: true,
            dataType: 'json',
            timeout: 15000,
            data: {originId: orgId},
            success: function (res) {
                // console.log(res)
                wapLoading.hide();
                if (res.code == 0) {
                    $('.user-orgName').html(convertVal(res.data.originFullName));
                    $('.TipsWindown').hide();
                } else {
                    wapAlert(res.msg);
                    return;
                }

            },
            error: function (res) {
                // wapAlert('系统异常,请稍后再试');
                wapLoading.hide();
                return;
            },
            complete: function (XMLHttpRequest, status) {
                if (status == 'timeout') {
                    wapAlert('网络链接超时,请稍后再试');
                    wapLoading.hide();
                    return;
                }
            }
        });
    });


    /**初始化站点**/
    function initOrigin() {
        $.ajax({
            type: "post",
            url: '/sys_origin/query/user/origin',
            async: true,
            dataType: 'json',
            timeout: 15000,
            data: {type: 2, status: 0},
            success: function (res) {
                if (res.code == 0) {
                    var html = '';
                    if (res.data != null && res.data.length > 0) {
                        $.each(res.data, function (index, val) {
                            if (index == 0) {
                                html += '<dt><span>' + val.originName + '</span> <i data-id="' + val.id +
                                    '" class="wnsM icon-confirm confirm-yes"></i></dt>';
                                orgId = val.id;
                            } else
                                html += '<dt><span>' + val.originName + '</span> <i data-id="' + val.id +
                                    '" class="wnsM icon-confirm"></i></dt>';
                        });
                        $('.station-list').empty().append(html);
                    } else {
                        $('.station-list').empty().append("<dt><span>您未关联站点,请联系管理人员</span></dt>");
                    }
                } else {
                    $('.station-list').empty().append("<dt><span>您未关联站点,请联系管理人员</span></dt>");
                }
            },
            error: function (res) {
                // wapAlert(res.msg);
                return;
            },
            complete: function (XMLHttpRequest, status) {
                if (status == 'timeout') {
                    wapAlert('网络链接超时,请稍后再试');
                    return;
                }
            }
        });
    }

    function initData() {
        $.ajax({
            type: "POST",
            url: "/user/center/init",
            dataType: 'json',
            timeout: 30000,
            success: function (res) {
                if(res.code==0){
                    var data = res.data;
                    console.log(data)
                    var url = convertVal(data.url);
                    var orgName = convertVal(data.orgName);
                    var phone = convertVal(data.phone);
                    /**权限信息0管理员，1销售人员**/
                    var role = convertNumber(data.role);
                    var roleList = data.roleList;
                    $('.real-name').html(convertVal(data.name));
                    $('.user-position').html(convertVal(data.position));
                    $('.user-orgName').html(convertVal(data.orgName));
                    if (orgName.length > 14)
                        $('.user-orgName').html(orgName.substring(0,14) + '...');
                    else
                        $('.user-orgName').html(orgName);

                    $('.role-menu').hide();
                    if(roleList.indexOf("系统管理员") > -1){
                        $('.role-menu').show();
                    }
                    if(roleList.indexOf("销售经理") > -1 || roleList.indexOf("客户经理") > -1
                        || roleList.indexOf("砂浆销售经理") > -1 || roleList.indexOf("销售专员") > -1 || roleList.indexOf("销售内业") > -1){
                        $('.role-1').show();
                    }
                    if(roleList.indexOf("调度") > -1 || roleList.indexOf("生产经理") > -1){
                        $('.role-2').show();
                    }
                    if(roleList.indexOf("总调") > -1){
                        $('.role-23').show();
                    }
                    if(roleList.indexOf("前场工长") > -1){
                        $('.role-24').show();
                    }
                    if(roleList.indexOf("备品备件负责人") > -1){
                        $('.role-29').show();
                    }
                    if(roleList.indexOf("备品备件主管") > -1){
                        $('.role-30').show();
                    }
                    /*if (role == 0){
                        // 管理员
                        $('.role-menu').show();
                    } else if (role == 1){
                        // 销售内业
                        $('.role-1').show();
                    } else if (role == 2){
                        // 销售
                        $('.role-2').show();
                    } else if(role == 23){
                        // 调度员
                        $('.role-23').show();
                    }else if(role == 24){
                        // 前场工长
                        $('.role-24').show();
                    }*/



                    if (phone.length > 0)
                        $('.user-phone').html(phone.substring(0,3) + '******' + phone.substring(9));

                    if (url.length > 0)
                        $('.headUrl').attr('src', url);
                }
            },
            error: function (res) {
                wapAlert("请求失败，请稍后再试！");
                return;
            },
            complete: function (XMLHttpRequest, status) {
                if (status == 'timeout') {
                    wapAlert("请求超时，请稍后再试！");
                    return;
                }
            }
        })
    }
});
